package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        int num = bookList.getBookNum();
        if(num == 0) {
            System.out.println("暂无图书，删除失败");
            return;
        }

        System.out.println("删除图书");
        System.out.println("请输入要删除的书名:");
        Scanner scan = new Scanner(System.in);
        String name = scan.nextLine();

        for(int i =0; i<num; ++i) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                for(int j=i; j<num-1; ++j){
                    Book nextBook = bookList.getBook(j+1);
                    bookList.setBook(nextBook,j);
                }
                bookList.setBookNum(num-1);
                System.out.println("删除成功!");
                return;
            }
        }
        System.out.println("没有这本书，删除失败");
    }
}
